#include <gtk/gtk.h>


static gboolean
fill (gpointer   user_data)
{
  GtkWidget *progress_bar = user_data;

  /* 获取当前进度条的值 */
  gdouble fraction;
  fraction = gtk_progress_bar_get_fraction (GTK_PROGRESS_BAR (progress_bar));

  /* 每次调用此函数时，将进度条增加10%，标准写法是fraction = fraction + 0.1 缩写为+= 0.1*/
  fraction += 0.1;

  /* 根据fraction的值设定进度条显示的长度 */
  gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (progress_bar), fraction);

  /* 确保进度条的值保持在1.0以下*/
  if (fraction < 1.0) 
    return TRUE;
  
  return FALSE;
}



static void
activate (GtkApplication *app,
          gpointer        user_data)
{
  GtkWidget *window;
  GtkWidget *progress_bar;

  gdouble fraction = 0.0;

  /* 创建具有标题和默认大小的窗口 */
  window = gtk_application_window_new (app);
  gtk_window_set_title (GTK_WINDOW (window), "ProgressBar Example");
  gtk_window_set_default_size (GTK_WINDOW (window), 220, 20);
 
  /* 创建进度条并将其添加到窗口 */
  progress_bar = gtk_progress_bar_new ();
  gtk_container_add (GTK_CONTAINER (window), progress_bar);

  /* 根据fraction的值设定进度条显示的长度，进度条的值必须介于0.0-1.0（含0.0-1.0）之间*/
  gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (progress_bar), fraction);

  /* 每隔500毫秒运行一次fill函数 */
  g_timeout_add (500, fill, GTK_PROGRESS_BAR (progress_bar));
 
  gtk_widget_show_all (window);
}
 


int
main (int argc, char **argv)
{
  GtkApplication *app;
  int status;
 
  app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
  g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
  status = g_application_run (G_APPLICATION (app), argc, argv);
  g_object_unref (app);
 
  return status;
}
